package com.example.entity;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import static com.example.util.FileReceiver.decryptAndSaveFile;
import static com.example.util.FileReceiver.receiveFile;
import static com.example.util.ProcessShowCommander.processShowCommand;

/**
 * @author pulinks
 */
public class Client {

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器已启动，等待客户端连接...");

            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端连接成功：" + clientSocket.getInetAddress().getHostAddress());

                // 接收文件
                String encryptedFilePath = receiveFile(clientSocket);

                // 解密文件
                String decryptedFilePath = decryptAndSaveFile(encryptedFilePath);

                // 处理show命令
                BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                processShowCommand(clientSocket, reader);

                clientSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}